<%--
  Created by IntelliJ IDEA.
  User: DongZhengWei
  Date: 2018/8/22
  Time: 16:30
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>

<div id="page-content">
	<div class="panel">
		<div class="panel-heading">
			<h3 class="panel-title">成员信息</h3>
		</div>
		<div class="panel-body">
			<table id="member_table" cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered"></table>
		</div>
	</div>
</div>

<script>

	$(function(){
		init_datatable_member();
	});

	function init_datatable_member(){
        var columns = [
            {targets : 0, data : "id", title : "id", visible : false, orderable : false, searchable : false},
	        {targets : 1, data : "name", title : "用户名称", orderable : true, render : nameRender},
	        {targets : 2, data : "sex", title : "性别", orderable : false, className : "min-tablet"},
	        {targets : 3, data : "account", title : "登陆账号", orderable : true},
	        {targets : 4, data : "accountType", title : "账户类型", visible : false, orderable : false},
	        {targets : 5, title : "职权信息", visible : true, orderable : false, searchable : false, render : roleBtnRender},
	        {targets : 6, data : "accountEnabled", title : "账户状态", visible : true, width : 80, orderable : false, searchable : false, render : stateBtnRender}
        ];
        var buttons = [
            {text : "新增成员", className : "btn btn-success", attr : {id : "add_member"}, enabled : true, action : add_member},
	        {text : "导出excel", extend : "excelHtml5", enabled : true}
        ];

        __init_dataTables("member_table","<%=path%>/main/member/list",columns,buttons,false,true)
	}

	function nameRender(data, type, row, meta){
		var accountType = row.accountType;
		var t = "";
		if(accountType === "Manager"){
			t = '&nbsp;&nbsp;<span class="label label-success">管</span>';
		}
		return data + t;
	}

	//职权信息按钮
	function roleBtnRender(data, type, row, meta){
	    var userId = row.id;
        return '<a href="javascript:void(0);" class="label label-table label-info" onclick="fn_user_duty_dialog(\'' + userId + '\')">职权信息</a>';
	}

	//账户状态按钮
	function stateBtnRender(data, type, row, meta){
	    var state = row.accountEnabled;
	    var isChecked = state===1 ? "checked" : "";
	    data = '<div class="ckbx-style-13 ckbx-medium">\n' +
            '<input class="stateCheckBox" type="checkbox" id="state-'+row.id+'" value="'+state+'" name="state-'+row.id+'" '+isChecked+'>\n' +
            '<label for="state-'+row.id+'"></label>\n' +
            '</div>';
		return data;
	}

	//添加成员
	function add_member(e,dt,node,config){
        __open_dialog_form("新增成员", "<%=path%>/main/member/input",function(dialogRef){
            var callback = function(data){
                if(data){
                    dialogRef.close();
                    $("#member_table").DataTable().state.clear();
                    $("#member_table").DataTable().ajax.reload();
                }else{
                    dialogRef.enableButtons();
                }
            };
            if(fn_member_input_save(callback) === false){
                dialogRef.enableButtons();
            }
        });
	}

	//职权信息
	function fn_user_duty_dialog(userId){
	    if(userId){
            __open_dialog_form("职权分配", "<%=path%>/main/member/dutyDialog?userId=" + userId, function(dialogRef){
	            var callback = function(data){
	                if(data && data.state === 1){
	                    dialogRef.close();
	                }else{
                        dialogRef.enableButtons();
	                }
	            };
	            fn_member_duty_save(callback);
            });
		}else{
	        __toastr_error("未找到人员信息");
		}
	}

	//账号状态修改
	$("#member_table").on("click",".stateCheckBox", function(){
	    var val = $(this).val();
	    var id = $(this).attr("id").split("-")[1];
	    __ajax_post("<%=path%>/main/member/editAccountEnabled", {val : val, userId : id}, function(data){
            $("#member_table").DataTable().ajax.reload(null, false);
	        if(data.state !== 1){
                __toastr_error(data.message);
	        }
	    })
	});

</script>